package likou.offer;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/8/12  22:32
 */
public class _19 {
    public static void main(String[] args) {
        System.out.println(isMatch("aab", "c*a*b*"));
    }

    public static boolean isMatch(String s, String p) {
        byte[] sb = s.getBytes();
        byte[] pb = p.getBytes();

        int nowP = 0;
        for (int i = 0; i < sb.length; i++) {
            if (nowP == pb.length) {
                return false;
            }
            if (pb[nowP] != '.' && sb[i] != pb[nowP] && pb[nowP + 1] != '*') {
                return false;
            }

            if (nowP < pb.length - 1 && pb[nowP + 1] == '*') {
                //遇到 .* 直接放行
                if (pb[nowP] == '.') {
                    if (nowP + 1 == pb.length - 1) {
                        return true;
                    } else {
                        return false;
                    }
                }
                while (i < sb.length && sb[i] == pb[nowP]) {
                    i++;
                }
                nowP++;
                nowP++;
                i--;
                continue;
            }
            if (pb[nowP] == '.') {
                nowP++;
                continue;
            }
            if (pb[nowP] == sb[i]) {
                nowP++;
                continue;
            }
        }
        if (nowP != pb.length) {
            return false;
        }
        return true;
    }
}
